#coding=utf-8
from wechatpy import parse_message, create_reply
from wechatpy.replies import TextReply, ArticlesReply
from wechatpy.utils import check_signature
from wechatpy.exceptions import InvalidSignatureException

import json,time,logging
from datetime import datetime
from message_lib import reply_msg
from func import get_client
from apps.userModel.models import User, Game, Travel, Record
from django.http import HttpResponseRedirect
from django.shortcuts import redirect, reverse, render

logger = logging.getLogger('mylog')

'''
    reply all msg from event
'''

def apply(msg):
    reply = None
    try:
        if msg.event == 'subscribe':
            reply = handler_subscribe(msg)
        elif msg.event == 'location':
            reply = handler_location(msg)
            #reply = create_reply('location reply', msg)
        elif msg.event == 'unsubscribe':
            reply = handler_unsubscribe(msg)
        elif msg.event == 'click':
            reply = handler_click(msg)
        elif msg.event == 'view':
            reply = handler_view(msg)
        elif msg.event == 'subscribe_scan':
            reply = handler_view(msg)
        elif msg.event == 'scan':
            reply = handler_view(msg)
    except Exception as e:
        print 'error:', e
        reply = None
    return reply

def handler_view(msg):
    '''
        点击菜单跳转链接事件
    '''
    reply = None
    try:
        openid = msg.source
        url = msg.url
            #return redirect(reverse('weixin.views.myrecs', args=[openid]))
        #logger.warn(openid + " " + url)
    except Exception as e:
        print 'error:', e
    return reply

def handler_click(msg):
    '''
        处理菜单点击事件
    '''
    reply = None
    try:
        openid = msg.source
        key = msg.key
        if key == 'MENU_HELP_CONTACT':
            reply = create_reply('menu contact', msg)
        elif key == 'MENU_HELP_STATEMENT':
            reply = create_reply('menu statement ', msg)
    except Exception as e:
        print 'error:', e
    return reply

def handler_subscribe(msg):
    '''
        关注公众号事件
    '''
    reply = None
    try:
        openid = msg.source
        client = get_client()
        nowuser = client.user.get(openid)
        user = User(openid=openid
                    , gender=nowuser['sex']
                    , imgurl=nowuser['headimgurl']
                    , wxname=nowuser['nickname']
                    , city=nowuser['city']
                    , country=nowuser['country']
                    , language=nowuser['language']
                    , province=nowuser['province']
                    , subscribe=1)
        user.save()

        # record = Record(gender=nowuser['sex']
        #             , imgurl=nowuser['headimgurl']
        #             , wxname=nowuser['nickname']
        #             )
        # Record.save()

        reply = create_reply('关注成功', msg)
    except Exception as e:
        print 'error:', e
    return reply

def handler_unsubscribe(msg):
    reply = None
    try:
        openid = msg.source
        # logger.warn('unsubscribe update success')
        # reply = create_reply('unsubscribe', msg)
    except Exception as e:
        print 'error:', e
    return reply

def handler_location(msg):
    reply = None
    try:
        openid = msg.source
        # logger.warn('location update success')
        #reply = create_reply(str(msg), msg)
    except Exception as e:
        print 'error:', e
    return reply

